package io.renren.modules.employment.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.renren.modules.employment.entity.*;
import io.renren.modules.sys.dto.PostionDto;
import io.renren.modules.employment.service.PostionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import io.renren.modules.employment.dao.PostionDao;
import io.renren.modules.employment.dao.WelfareDao;

@Service
public class PostionServiceImpl implements PostionService {

	@Autowired
	PostionDao postionDao;
	
	@Autowired
	WelfareDao welfareDao;
	/**
	 * 查询所有的岗位信息
	 */
	@Override
	public List<CompanyAndPostion> getAllPostion() {
		List<CompanyAndPostion> postionList = postionDao.getAllPostion();
		for(CompanyAndPostion p:postionList)
		{
			String w=p.getWelfareIds();
			p.setWelfareList(welfareDao.getWelfareListByIds(p.getWelfareIds()));
		}
		return postionList;
	}
	
	/**
	 * 根据id获取岗位信息
	 */
	@Override
	public Position getPostionById(String id) {
		Position postion = postionDao.selectById(id);
		return postion;
	}

	/**
	 * 根据外键查询当前岗位的公司信息
	 */
	@Override
	public Company getCompanyByPostion2(Postion2 postion) {
		Company company = postionDao.getCompanyByPostion2(postion);
		return company;
	}

	/**
	 * 查询当前公司下的其他职位的方法
	 */
	@Override
	public List<CompanyAndPostion> getPostionNotIncludeThisPostionByCompany(PostionDto postion) {
		List<CompanyAndPostion> postionList = postionDao.getPostionNotIncludeThisPostionByCompany(postion);
		for(CompanyAndPostion p:postionList)
		{
			String w=p.getWelfareIds();
			p.setWelfareList(welfareDao.getWelfareListByIds(p.getWelfareIds()));
		}
		
		return postionList;
	}

	/**
	 * 根据岗位的id查询福利的List<welfare>
	 */
	@Override
	public List<Welfare> getWelfareByPostionId(Postion2 postion) {
		List<Welfare> list = welfareDao.getWelfareByPostionId(postion);
		return list;
	}

	@Override
	public List<Postion2> getPostionByLike(String postionTitle) {
		return postionDao.getPostionByLike(postionTitle);
	}

	@Override
	public List getPosts(Map map) {
		Long userId = Long.valueOf(map.get("userId").toString());
		int parentId = Integer.valueOf(map.get("parentId").toString());
		//首先获取第一层分类
		List firstPostList = postionDao.getFirstPost(map);
		for (int i =0;i<firstPostList.size();i++){
			PostAndIndustry postAndIndustry = (PostAndIndustry)firstPostList.get(i);
			int id = postAndIndustry.getId();
			map.put("parentId",id);
			List secondPostList = postionDao.getSecondPost(map);
			//首先获取当前用户收藏的岗位
			PostUser postUser = postionDao.getUserPosts(userId);

			if(null == postUser || "".equals(postUser)){
				//没有收藏岗位标签
				postAndIndustry.setPostList(secondPostList);
			}
			else{
				//有岗位标签的收藏
				String  userPostId= postUser.getPostId();
				for(int s = 0;s<secondPostList.size();s++){
					PostAndIndustry2 postAndIndustry2 = (PostAndIndustry2)secondPostList.get(s);
					postAndIndustry2.setIsChoose(0);
					//获取当前岗位的id
					int postAndIndustry2Id = postAndIndustry2.getId();
					String[] userPostIdList = userPostId.split(",");
					for(int x =0;x<userPostIdList.length;x++){
						int intUserPostId = Integer.valueOf(userPostIdList[x]);
						if(intUserPostId == postAndIndustry2Id){
							postAndIndustry2.setIsChoose(1);
						}
					}
				}
				postAndIndustry.setPostList(secondPostList);
			}
		}
		return firstPostList;
	}

	@Override
	public void insertUserPosts(Map map) {
		postionDao.insertUserPosts(map);
	}

	@Override
	public PostUser getUserPosts(Long userId) {
		return postionDao.getUserPosts(userId);
	}

	@Override
	public PostAndIndustry2 getUserPostById(int postId) {
		return postionDao.getUserPostById(postId);
	}

	@Override
	public void updateUserPosts(Map map) {
		postionDao.updateUserPosts(map);
	}

}
